import Area from './Area.js';
import PixPoint from './PixPoint.js';
import Vector2 from './Vector2.js';
export default class Sprite extends Area {
    constructor(x = 0, y = 0, width = 0, height = 0, imgPath = '') {
        super(x, y, width, height);
        this.outBorder = new Array();
        this.img = new Image();
        this.img.src = imgPath;
    }
    initOutBorder(context) {
        this.img.onload = () => {
            context.drawImage(this.img, this.x, this.y, this.width, this.height);
            let imgData = context.getImageData(this.x, this.y, this.width, this.height);
            let bitArr = imgData.data;
            let pixArr = new Array();
            for (let i = 0; i < bitArr.length; i += 4) {
                let x = (i / 4) % this.width;
                let y = Math.floor(i / 4 / this.width);
                pixArr.push(new PixPoint(x, y, bitArr[i], bitArr[i + 1], bitArr[i + 2], bitArr[i + 3]));
            }
            this.outBorder = pixArr.filter((e, i, arr) => {
                var _a, _b, _c, _d;
                return ((((_a = arr[i - 1]) === null || _a === void 0 ? void 0 : _a.a) === 0 && e.a !== 0) ||
                    (((_b = arr[i + 1]) === null || _b === void 0 ? void 0 : _b.a) === 0 && e.a !== 0) ||
                    (arr[i - this.width] && ((_c = arr[i - this.width]) === null || _c === void 0 ? void 0 : _c.a) === 0 && e.a !== 0) ||
                    (arr[i + this.width] && ((_d = arr[i + this.width]) === null || _d === void 0 ? void 0 : _d.a) === 0 && e.a !== 0) ||
                    (!arr[i - this.width] && e.a !== 0) ||
                    (!arr[i + this.width] && e.a !== 0) ||
                    (!arr[i - 1] && e.a !== 0) ||
                    (!arr[i + 1] && e.a !== 0));
            })
                .map(e => new Vector2(e.x, e.y));
            // context.clearRect(this.x, this.y, this.width, this.height)
        };
    }
    // showBorder(context: CanvasRenderingContext2D) {
    //   this.outBorder.forEach(e => {
    //     context.fillStyle = `rgba(${e.r},${e.g},${e.b},${e.a})`
    //     context.fillRect(this.x + e.x, this.y + e.y, 1, 1)
    //   })
    // }
    draw(context) {
        context.drawImage(this.img, this.x, this.y, this.width, this.height);
    }
    onCollision(other) {
        let result = false;
        for (let i = 0; i < this.outBorder.length; i++) {
            if (result)
                break;
            for (let j = 0; j < other.outBorder.length; j++) {
                if (this.outBorder[i].offset(this.x, this.y).compare(other.outBorder[j].offset(other.x, other.y))) {
                    // console.log(this.outBorder[i].string,other.outBorder[j].string)
                    result = true;
                    break;
                }
            }
        }
        return result;
    }
}
